package com.twohand.admin.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.twohand.admin.Dto.BillDetailListDto;
import com.twohand.admin.Dto.WxPreOrderDto;
import com.twohand.admin.Entity.Qrcode;
import com.twohand.admin.Entity.WxOrder;
import com.twohand.admin.Service.BillService;
import com.twohand.admin.Service.OrderService;
import com.twohand.admin.Service.QrcodeService;
import com.twohand.admin.Service.WxOrderService;
import com.twohand.admin.Service.wechat.MpService;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.BillDetailOrderVo;
import com.twohand.admin.Vo.BillSearchResult;
import com.twohand.admin.Vo.WxPayOrderNotifyCallbackResultVo;
import com.twohand.utils.Response.BaseException;
import com.twohand.utils.Response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
@RequestMapping("/api/h5")
public class WebApiController {
    @Autowired
    BillService billService;
    @Autowired
    OrderService orderService;
    @Autowired
    QrcodeService qrcodeService;
    @Autowired
    WxOrderService wxOrderService;
    @Autowired
    MpService mpService;
    @GetMapping("/billDetailList")
    PageResponse<BillSearchResult> getBillDetailList(BillDetailListDto billDetailListDto) throws ParseException {
        if(billDetailListDto.getShopName()==null){
            throw  new BaseException(500,"参数错误");
        }
        billDetailListDto.setId(Integer.valueOf(billDetailListDto.getShopName()));
        return billService.getBillDetailList(billDetailListDto);
    }

    @GetMapping("/code")
    Qrcode getDefaultCode(){
        return qrcodeService.getDefault();
    }
    @GetMapping("/billDetail")
    BillDetailOrderVo getBillDetail(String billNo){
       return billService.getBillDetail(billNo);
    }
//    @GetMapping("/test")
//    Object getIp(String orderNo){
//        return wxOrderService.test(orderNo);
//    }
    @GetMapping("/block/{orderNo}")
    Boolean block(@PathVariable String orderNo){
        return orderService.updateBlock(orderNo);
    }
    @GetMapping("/code2Token")
    Response getOpenId(String code) throws JsonProcessingException {
        return mpService.code2OpenId(code);
    }
    @PostMapping("/preOrder")
    WxOrder preOrder(@RequestBody WxPreOrderDto wxPreOrderDto) throws WxPayException {
        return wxOrderService.preCreateOrder(wxPreOrderDto);
    }
    @PostMapping("/wxPayNotify")
    public WxPayOrderNotifyCallbackResultVo payNotify(@RequestBody String notifyData,
                                                      @RequestHeader("Wechatpay-Nonce") String nonce,
                                                      @RequestHeader("Wechatpay-Timestamp") String timestamp,
                                                      @RequestHeader("Wechatpay-Serial") String serial,
                                                      @RequestHeader("Wechatpay-Signature") String signature){
        return wxOrderService.payNotify(notifyData,nonce,timestamp,serial,signature);
    }

    @PostMapping("/wxRefundNotify")
    public WxPayOrderNotifyCallbackResultVo refundNotify(@RequestBody String notifyData,
                                                         @RequestHeader("Wechatpay-Nonce") String nonce,
                                                         @RequestHeader("Wechatpay-Timestamp") String timestamp,
                                                         @RequestHeader("Wechatpay-Serial") String serial,
                                                         @RequestHeader("Wechatpay-Signature") String signature){
        return wxOrderService.refundNotity(notifyData,nonce,timestamp,serial,signature);
    }
}
